import { defineStore } from 'pinia';
import {
  getCustomers,
  addCustomer,
  updateCustomer,
  deleteCustomer,
  addPet,
  updatePet,
  deletePet,
  addVaccine,
  updateVaccine,
  deleteVaccine,
  getCareList,
  addCare,
  updateCare,
  deleteCare,
  sendCare,
  batchGenerateCare,
  upgradeMember
} from '@/api/customer';

export const useCustomerStore = defineStore('customer', {
  state: () => ({
    customers: []
  }),
  actions: {
    async fetchCustomers(params = {}) {
      console.log('[Store] fetchCustomers action 参数:', params);
      const res = await getCustomers(params);
      this.customers = res.data?.data || [];
      console.log('[Store] fetchCustomers this.customers:', this.customers);
      return res;
    },
    async addCustomer(customer) {
      console.log('[Store] addCustomer action 参数:', customer);
      const res = await addCustomer(customer);
      await this.fetchCustomers();
      return res;
    },
    async updateCustomer(id, data) {
      console.log('[Store] updateCustomer action 参数:', id, data);
      const res = await updateCustomer(id, data);
      await this.fetchCustomers();
      return res;
    },
    async deleteCustomer(id) {
      console.log('[Store] deleteCustomer action 参数:', id);
      const res = await deleteCustomer(id);
      await this.fetchCustomers();
      return res;
    },
    // 宠物相关
    async addPet(customerId, data) {
      const res = await addPet(customerId, data);
      await this.fetchCustomers();
      return res;
    },
    async updatePet(id, data) {
      const res = await updatePet(id, data);
      await this.fetchCustomers();
      return res;
    },
    async deletePet(id) {
      const res = await deletePet(id);
      await this.fetchCustomers();
      return res;
    },
    // 疫苗相关
    async addVaccine(data) {
      const res = await addVaccine(data);
      await this.fetchCustomers();
      return res;
    },
    async updateVaccine(data) {
      const res = await updateVaccine(data);
      await this.fetchCustomers();
      return res;
    },
    async deleteVaccine(id) {
      const res = await deleteVaccine(id);
      await this.fetchCustomers();
      return res;
    },
    // 关怀相关（如需全局管理可加）
    async fetchCareList(params = {}) {
      return await getCareList(params);
    },
    async addCare(data) {
      return await addCare(data);
    },
    async updateCare(data) {
      return await updateCare(data);
    },
    async deleteCare(id) {
      return await deleteCare(id);
    },
    async sendCare(id) {
      return await sendCare(id);
    },
    async batchGenerateCare(data) {
      return await batchGenerateCare(data);
    },
    // 升级会员
    async upgradeMember(data) {
      return await upgradeMember(data);
    }
  }
}); 
